package com.etlmaster.executor.steps.table;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.StepState;
import com.etlmaster.jdbc.JdbcTemplate;

/**
 * @author Wugf
 * @date 2016-03-08
 */
@StepInstruction("emptyTable")
public class EmptyTableStep extends Step {

	private EmptyTableStepMeta meta;
	
	public EmptyTableStep(StepMeta meta) {
		super(meta);
		this.meta=(EmptyTableStepMeta)meta;
		// TODO Auto-generated constructor stub
	}

	
	@Override
	protected int handle() {
		int flag=StepState.RUNNING_SUCCESSED;
		//meta.getdbInfo();
		try{
			JdbcTemplate jdbcTemplate = new JdbcTemplate(meta.getDsName());
			
			String dsType = DataCollectionUtils.getDsType(meta.getDsName());
			StringBuilder sql=new StringBuilder();
			String realTableName=meta.variableSubstitution(meta.getTableName());
			int row = 0;
			//hive需要调用excute方法
			if(!dsType.equals("db2")){
				sql.append("truncate table ").append(realTableName);
				meta.log("INFO","清空表{}",sql.toString());
				jdbcTemplate.execute(sql.toString());
			}else{
				sql.append("ALTER TABLE ")
				   .append(realTableName)
				   .append(" ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE");
				meta.log("INFO","清空表{}",sql.toString());
				row=jdbcTemplate.update(sql.toString());
			}
		}catch(Exception e){
			meta.log("ERROR",e.getMessage());
			flag=StepState.RUNNING_FAILED;
		}
		
		return flag;
		//return SqlProcessor.getSqlProcessor(meta,this).process();
	}



	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}
	

}
